package com.esunny.springboot.filter;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器在拦截器前面（它是基于servlet的）
 * 主要是过滤一些参数或者请求等.注意如果加了security，这个过滤器不会被执行
 */

@Configuration
public class MyFileter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 实际
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        System.out.println("进入过滤器");
        filterChain.doFilter(servletRequest, servletResponse);


        // HttpSession session = httpServletRequest.getSession();
        // // 请求中没有这个参数
        // if (session.getAttribute("login") == null) {
        //     // 没有登陆，重定向到登录页面
        //     httpServletResponse.sendRedirect("/user/login");
        // } else {
        //     System.out.println("ip:" + httpServletRequest.getRemoteHost() + ",url:" + httpServletRequest.getRequestURL());
        //     // 继续执行后续逻辑
        //     filterChain.doFilter(servletRequest, servletResponse);
        // }

    }

    @Override
    public void destroy() {

    }
}